<template>
<div class="page add-member">
        <div class="weui-cells weui-cells_form">
            <a class="weui-cell weui-cell_access" href="javascript:;" @click="gotoEditDepartmentName">
                <div class="weui-cell__bd">
                    <p><span class="title">部门名称</span><span>{{currGroupName}}</span></p>
                </div>
                <div class="weui-cell__ft">
                </div>
            </a> 
            <a class="weui-cell weui-cell_access" href="javascript:;" @click="selectGroup">
                <div class="weui-cell__bd">
                    <p><span class="title">上级部门</span><span>{{prevGroupName}}</span></p>
                </div>
                <div class="weui-cell__ft">
                </div>
            </a> 
        </div>
    </div> 
</template>

<script>
    /**
     *  引入工具类库 或 Native API
     *  example : import widget from 'gapi/widget'
     */
    import $ from 'zepto'
    import * as util from 'js/util.js'
    import cache from 'gapi/cache'
    import baseApi from 'gapi/base'
    import clientInfo from 'gapi/clientInfo'
    import widget from 'gapi/widget'
    import commonPage from 'clientApi/commonPage.js'
    /**
     *  引入组件 以及 组件接口(Action)
     *  example : import Somecomponents from 'components/Somecomponents/Somecomponents'
     */

    /**
     *  组件实例对象
     */
    export default {
        components: {

        },
        data: function(){
            return{
                teamid:'', 
                department_id:'',  // 部门id
                currGroupName:'',  // 当前编写的小组名称
                parent_id:'',
                prevGroupName:'',  // 上级小组名称
                old_prev_name:'',
                new_prev_name:'',
                delBackWebViewId:'',
                dataChane_curname:false,
                dataChane_prevname:false,
            }
        },
        vuex: {
            getters: {

            },
            actions: {

            }
        },
        methods: {
            /**
             * 初始化
             * @method init
             * @public
             * @return {Null} void
             */
            init (){
                this.teamid = util.getUrddataString('teamId')
                this.department_id = util.getUrddataString('department_id')
                this.delBackWebViewId = util.getUrddataString('webViewId')
                commonPage.getWebViewID((data)=>{
                    cache.setMemoryCache('editDepartmentWebViewID',{'webViewID':data})
                })
                this.getGroupData()
            },
            // 编辑部门名称
            gotoEditDepartmentName (){
                let str = `groupName=${this.currGroupName}`
                let encodeStr = encodeURIComponent(str)
                location.href = `dmsaas://EditDepartementName?urdData=${encodeStr}`
            },
            // 上级部门选择
            selectGroup(){
                let str = `department_id=${this.department_id}&teamid=${this.teamid}`
                let encodeStr = encodeURIComponent(str)
                location.href = `dmsaas://TeamDepartmentSelect?urdData=${encodeStr}`
            },
            // 获取部门信息
            getGroupData(){
                let _data = {
                        team_id:this.teamid,
                        department_id:this.department_id
                    }
                console.log(_data)
                commonPage.loadPageStatus('loading')
                util.ajax({
                    url:'/team/department_info',
                    type:'GET',
                    data:_data,
                    timeout:10000,
                    success:(obj) => {
                        console.log(obj)
                        commonPage.loadPageStatus('loadSuccess')
                        if(obj && obj.errno == 0){
                            this.prevGroupName = obj.data.parent_name
                            this.currGroupName = obj.data.name
                            this.parent_id = obj.data.parent_id

                            this.old_prev_name = obj.data.parent_name
                        }
                    },
                    error: (xhr, status) => {
                        commonPage.loadPageStatus('loadSuccess')
                        widget.toast('服务器异常')
                    },
                    noNetwork: () => {
                        commonPage.loadPageStatus('loadSuccess')
                        widget.toast('网络连接失败，请检查网络')
                    }
                })
            },
            // 更新小组
            updateData(data){
                console.log(data)
                if(data){
                    let pushData = ''
                    if(data.key == 'editDepartmentName'){ // 修改名称
                        pushData = {
                            team_id:this.teamid,
                            department_id:this.department_id,  //部门id
                            name:data.value.name,
                            parent_id:this.parent_id   ///父部门id
                        }
                    }else if(data.key == 'selectGroup'){  // 修改上级部门
                        pushData = {
                            team_id:data.value.team_id,
                            department_id:this.department_id,  //部门id
                            name:this.currGroupName,
                            parent_id:data.value.id   ///父部门id
                        }
                    }
                    console.log(pushData)
                    commonPage.loadPageStatus('loading')
                    util.ajax({
                        url:'/team/department/update',
                        type:'POST',
                        data:pushData,
                        timeout:10000,
                        success:(obj) => { 
                            // console.log(obj)
                            commonPage.loadPageStatus('loadSuccess')
                            if(obj&&obj.errno == 0){
                                if(data.key == 'editDepartmentName'){
                                    this.currGroupName = data.value.name
                                }else if(data.key == 'selectGroup'){
                                    this.prevGroupName = data.value.name
                                    this.parent_id = data.value.id
                                    this.new_prev_name = data.value.name
                                }
                                widget.toast('修改成功')
                            }else{
                                widget.toast(obj.errmsg)
                            }
                        },
                        error: (xhr, status) => {
                            commonPage.loadPageStatus('loadSuccess')
                            widget.toast('服务器异常')
                        },
                        noNetwork: () => {
                            commonPage.loadPageStatus('loadSuccess')
                            widget.toast('网络连接失败，请检查网络')
                        }
                    })
                }
                this.doWatch()   //检测数据是否改变
            },
            doWatch (){
                this.$watch('currGroupName', (newVal, oldVal) => {
                    if(newVal != oldVal){
                        this.dataChane_curname = true
                    }else{
                        this.dataChane_curname = false
                    }
                })
                this.$watch('prevGroupName', (newVal, oldVal) => {
                    if(newVal != oldVal){
                        this.dataChane_prevname = true
                    }else{
                        this.dataChane_prevname = false
                    }
                })
            }
           
        },
        created(){
            
            this.init();
            //控制header
            widget.updateTitleBar({
               backAction: 'goBack',
               leftAction: 'goBack',
            })
            cache.getMemoryCache('teamGroupListWebViewID', (data) => {
                console.log(data)
            })
            window.goBack = (data) => {
                console.log(data)
                console.log(this.delBackWebViewId)
                if(data && data == 1){
                    if(this.delBackWebViewId){
                      commonPage.goBack(1, {}, this.delBackWebViewId)
                    }
                    // cache.getMemoryCache('teamGroupListWebViewID', (data) => {
                    //     console.log(data)
                    // })
                }else{
                    if(this.dataChane_curname == true && this.dataChane_prevname == false){
                        commonPage.goBack(1,{'groupName':this.currGroupName})
                    }else if(this.dataChane_prevname == true && this.dataChane_curname == false){
                        cache.getMemoryCache('teamGroupListWebViewID', (data) => {
                            if(data){
                                commonPage.goBack(1,{},data.webViewID)
                            } 
                        })
                    }else if(this.dataChane_curname == true && this.dataChane_prevname == true){
                        cache.getMemoryCache('teamGroupListWebViewID', (data) => {
                            if(data){
                                commonPage.goBack(1,{},data.webViewID)
                            } 
                        })
                    }else if(this.dataChane_curname == false && this.dataChane_prevname == false){
                        commonPage.goBack(1,{'groupName':this.currGroupName})
                    }
                }
                
                 
            }
            window.loadData = this.updateData
            commonPage.getWebViewID((data)=>{
                cache.setMemoryCache('selectDepartmentWebViewID',{'webViewID':data})
            })
        }
    }


</script>

<!-- CSS 样式 -->
<style src="../../node_modules/weui/dist/style/weui.css"></style>
<style src='../../static/css/common.css'></style>
<style src='../../static/css/user-list.css'></style>
